package io.gitee.kotle.demo.mvvm_demo.pic

import android.graphics.Rect
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import io.gitee.kotle.android.delegate.viewModelDelegate
import io.gitee.kotle.android.library.banner.IBanner
import io.gitee.kotle.android.mvvm.mvvminterface.MvvmViewType
import io.gitee.kotle.android.utils.*
import io.gitee.kotle.base.library.BaseFragment


/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/24 5:43 下午
 *
 */
class BannerFragment : BaseFragment() {
    private lateinit var root: ViewGroup
    override fun onMvvmCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): MvvmViewType? {
        return MvvmViewType.ViewInstanceType(FrameLayout(inflater.context).apply {
            root = this
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200.dp)
        })
    }

    private val picViewModel by viewModelDelegate<PicViewModel>()

    override fun initUi(savedInstanceState: Bundle?) {
        super.initUi(savedInstanceState)
        val banner = IBanner<IBanner.BannerData>(root)
        picViewModel.picData.observerAttachView {
            banner.setRepeatCount(-1)
            banner.setAutoScrollDuration(200)
            banner.setAutoScrollInterval(200)
            banner.setItemMargin(Rect(100, 0, 100, 0))
            banner.setItemPadding(Rect(200, 0, 200, 0))
            banner.setListener(object : IBanner.Listener<IBanner.BannerData> {
                override fun onBannerClick(
                    bannerItemView: View,
                    bannerData: IBanner.BannerData,
                    position: Int
                ) {
                    super.onBannerClick(bannerItemView, bannerData, position)
                    "$position".toast()
                }
            })
            postMainHandler(2000) {
                banner.setDatas(
                    it.currentPageList!!.map {
                        object : IBanner.BannerData {
                            override fun getBannerUrl(): Any {
                                return (it as PicBean.ImagesDTO).url
                            }
                        }
                    },
                    isCanInfiniteScroll = false
                )
            }
        }
    }

    override fun initData() {
        super.initData()
        picViewModel.loadPic()
    }


    override fun isNeedBaseUiView(): Boolean {
        return true
    }
}